import requests
from bs4 import BeautifulSoup
from resume.models import ResumeTemplate

def crawl_500d_templates():
    headers = {'User-Agent': 'Mozilla/5.0'}
    base_url = 'https://www.500d.me/cvresume/'
    resp = requests.get(base_url, headers=headers)
    soup = BeautifulSoup(resp.text, 'html.parser')
    # 1. 获取所有类型的入口链接
    topic_links = soup.select('ul#topic-owl li a')
    for a in topic_links:
        topic_url = a['href']
        if not topic_url.startswith('http'):
            topic_url = 'https://www.500d.me' + topic_url
        topic_name = a['alt'] if a.has_attr('alt') else ''
        print(f'正在采集类型: {topic_name} {topic_url}')
        # 2. 采集该类型下的所有模板
        topic_resp = requests.get(topic_url, headers=headers)
        topic_soup = BeautifulSoup(topic_resp.text, 'html.parser')
        cards = topic_soup.select('.template-common-cards.hover')
        print(f'本类型共采集到{len(cards)}个模板')
        for card in cards:
            name = card.select_one('.template-name').get_text(strip=True)
            img = card.select_one('img')['data-original']
            detail_url = card.select_one('a.make-btn')['href']
            if not detail_url.startswith('http'):
                detail_url = 'https://www.500d.me' + detail_url
            print('模板:', name, img, detail_url)
            # 采集详情页
            detail_resp = requests.get(detail_url, headers=headers)
            detail_soup = BeautifulSoup(detail_resp.text, 'html.parser')
            desc = detail_soup.select_one('.resume-desc').get_text(strip=True) if detail_soup.select_one('.resume-desc') else ''
            download_btn = detail_soup.select_one('.resume-download-btn')
            download_url = download_btn['href'] if download_btn and download_btn.has_attr('href') else ''
            try:
                obj, created = ResumeTemplate.objects.update_or_create(
                    name=name,
                    defaults={
                        'preview_img': img,
                        'desc': desc,
                        'download_url': download_url,
                        'template_type': '',  # 可扩展
                        'tags': '',           # 可扩展
                        'source': '500丁'
                    }
                )
                print(f'{"新增" if created else "更新"}：{name}')
            except Exception as e:
                print('写入数据库出错:', e)